package com.irdstudio.tdp.console.api.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.irdstudio.sdk.beans.core.util.UUIDUtil;
import com.irdstudio.tdp.console.service.facade.OsrvArrangeVarService;
import com.irdstudio.tdp.console.service.vo.OsrvArrangeVarVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.irdstudio.tdp.console.service.facade.OsrvArrangeTableService;
import com.irdstudio.tdp.console.service.vo.OsrvArrangeTableVO;
import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.beans.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class OsrvArrangeTableController extends AbstractController  {
	
	@Autowired
	@Qualifier("osrvArrangeTableServiceImpl")
	private OsrvArrangeTableService osrvArrangeTableService;

	@Autowired
	@Qualifier("osrvArrangeVarServiceImpl")
	private OsrvArrangeVarService osrvArrangeVarService;

	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/tables", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<OsrvArrangeTableVO>> queryOsrvArrangeTableAll(
			OsrvArrangeTableVO vo) {		
		List<OsrvArrangeTableVO> outputVo = osrvArrangeTableService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/table/{tableRowId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<OsrvArrangeTableVO> queryByPk(@PathVariable("tableRowId") String tableRowId) {		
		OsrvArrangeTableVO inVo = new OsrvArrangeTableVO();
				inVo.setTableRowId(tableRowId);
		OsrvArrangeTableVO outputVo = osrvArrangeTableService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param osrvArrangeTable
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/table", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody OsrvArrangeTableVO inOsrvArrangeTableVo) {		
		int outputVo = osrvArrangeTableService.deleteByPk(inOsrvArrangeTableVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inOsrvArrangeTableVo
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/table", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody OsrvArrangeTableVO inOsrvArrangeTableVo) {		
		int outputVo = osrvArrangeTableService.updateByPk(inOsrvArrangeTableVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inOsrvArrangeTableVo
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/table", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertOsrvArrangeTable(@RequestBody OsrvArrangeTableVO inOsrvArrangeTableVo) {
		int outputVo = osrvArrangeTableService.insertOsrvArrangeTable(inOsrvArrangeTableVo);
		return getResponseData(outputVo);
		
	}

	/**
	 *  同时根据 appModelId 查询 变量列表 与 表格 数据
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/data", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Map> queryOsrvArrangeData(@RequestBody OsrvArrangeTableVO vo) {
		Map<String, Object> result = new HashMap<>();
		List<OsrvArrangeTableVO> tables = osrvArrangeTableService.queryAllOwnerNoPage(vo);
		result.put(OsrvArrangeTableVO.class.getSimpleName(), tables);

		OsrvArrangeVarVO varParam = new OsrvArrangeVarVO();
		varParam.setAppModelId(vo.getAppModelId());
		List<OsrvArrangeVarVO> vars = osrvArrangeVarService.queryAllOwnerNoPage(varParam);
		result.put(OsrvArrangeVarVO.class.getSimpleName(), vars);

		return getResponseData(result);
	}

	/**
	 *  保存 table 列表
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/osrv/arrange/savelist", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Boolean> saveTableData(@RequestBody List<OsrvArrangeTableVO> list) {

		// 为空 默认成功
		if (CollectionUtils.isEmpty(list)) {
			return getResponseData(true);
		}

		Boolean flag = true;
		for (OsrvArrangeTableVO t : list) {
			if (StringUtils.isBlank(t.getTableRowId())) {
				t.setTableRowId(UUIDUtil.getUUID());
				osrvArrangeTableService.insertOsrvArrangeTable(t);
			} else {
				// 检查是否前端生成的主键
				OsrvArrangeTableVO checkParam = new OsrvArrangeTableVO();
				checkParam.setTableRowId(t.getTableRowId());
				OsrvArrangeTableVO checkObj = osrvArrangeTableService.queryByPk(checkParam);
				if (checkObj == null) {
					osrvArrangeTableService.insertOsrvArrangeTable(t);
				} else {
					osrvArrangeTableService.updateByPk(t);
				}
			}
		}
		return getResponseData(true);
	}
}
